package com.sgu6.sgu6_machine.ui.activity;

import android.graphics.Color;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.DialogFragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import android.view.MenuItem;
import android.widget.EditText;

import com.sgu6.middleware.util.HexUtil;
import com.sgu6.serialport.manager.SerialPortManager;
import com.sgu6.sgu6_machine.R;
import com.sgu6.sgu6_machine.base.BaseA;
import com.sgu6.sgu6_machine.util.ToastUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class HandCommandA extends BaseA {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.etCommand)
    EditText etCommand;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_hand_command);
        ButterKnife.bind(this);
        initUI();
    }

    @Override
    public void initUI() {
        initToolbar();
    }

    @OnClick(R.id.btnSend)
    public void onViewClicked() {
        String command = etCommand.getText().toString().trim();
        if (command.length() / 2 == 0) {
            SerialPortManager.getInstance().sendBytes(HexUtil.HexString2Bytes(command));
        } else {
            ToastUtil.show(this, "命令长度不对");
        }
    }

    private void initToolbar() {
        toolbar.setTitle("手动发送命令");
        toolbar.setTitleTextColor(Color.WHITE);
        toolbar.setBackgroundResource(R.color.colorPrimaryDark);
        setSupportActionBar(toolbar);

        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowTitleEnabled(true);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
        }
        return true;
    }
}
